using Godot;
using System;

public partial class Start : Node2D
{
    [Export] AnimationPlayer animationPlayer;
    [Export] ColorRect colorRect;
    public Node dialogs;
    public override void _Ready()
    {
        // Play fade animation, then start dialog when it finishes
        if (animationPlayer != null)
        {
            animationPlayer.AnimationFinished += OnAnimationFinished;
            animationPlayer.Play("fade");
        }

        // Also listen for dialog end and custom signals to change scene
        var dialogic = GetNodeOrNull<Node>("/root/Dialogic");
        if (dialogic != null)
        {
            dialogic.Connect("timeline_ended", new Callable(this, nameof(OnDialogicTimelineEnded)));
            dialogic.Connect("signal_event", new Callable(this, nameof(OnDialogicSignal)));
        }
    }
    void changes()
    { 
         GetTree().ChangeSceneToFile("res://assent/tscn/game.tscn");
    }
    public override void _PhysicsProcess(double delta)
    {
        // No-op

    }

     private void StartTimeline111()
    {
        // Get the Dialogic autoload and start the timeline111 dialog
        var dialogic = GetNode<Node>("/root/Dialogic");
        if (dialogic != null)
        {
            dialogic.Call("start", "timeline111");
            GD.Print("Starting timeline111 dialog");
        }
        else
        {
            GD.PrintErr("Dialogic not found! Make sure it's properly configured in project.godot");
        }
    }

    private void OnAnimationFinished(StringName animationName)
    {
        if (animationName == "fade")
        {
            StartTimeline111();
        }
        else if (animationName == "zhayan")
        {
            changes();
        }
    }

    private void OnDialogicSignal(string argument)
    {
        if (argument == "changestwo")
        {
            GD.Print("Received changestwo signal, stopping dialog and changing to game scene");
            var dialogic = GetNode<Node>("/root/Dialogic");
            if (dialogic != null)
            {
                dialogic.Call("end_timeline");
            }
            animationPlayer.Play("zhayan");
        }
    }

    private void OnDialogicTimelineEnded()
    {
        changes();
    }
}
